﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data;
using System.Windows.Data;
using VAdvantage.Model;
using VAdvantage.Utility;

namespace VAdvantage.Framework
{
    public partial class LocationSet : ChildWindow
    {
        #region PrivateVariable
        MRegion region = null;
        MCountry country = null;
        private string postal = null;
        private string name = null;
        private string subname = null;
        private string city =null;
        #endregion

        #region Constructor
        public LocationSet()
        {
            InitializeComponent();
        }

        public LocationSet(DataSet ds)
        {
            InitializeComponent();
            BindingSource source = new BindingSource(ds.Tables[0].GetBindableData(new Connector()));
            vdgvLocationSet.AutoGenerateColumns = true;
            vdgvLocationSet.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(vdgvLocationSet_AutoGeneratingColumn);
            vdgvLocationSet.ItemsSource = source;
        }

        
        #endregion

        #region Events
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (vdgvLocationSet.SelectedItems.Count == 0)
            {
                return;
            }

            BindingSource vdata = vdgvLocationSet.ItemsSource as BindingSource;
            int index = vdgvLocationSet.SelectedIndex;

            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {

                if (vdata.Count > 0)
                {

                    if (((BindableObject)vdata[index]).GetValue("C_REGION_ID") != null && ((BindableObject)vdata[index]).GetValue("C_REGION_ID").ToString() != "")
                    {
                        region = new MRegion(Envs.GetCtx(), Convert.ToInt32(((BindableObject)vdata[index]).GetValue("C_REGION_ID")), null);
                    }
                    if (((BindableObject)vdata[index]).GetValue("C_COUNTRY_ID") != null && ((BindableObject)vdata[index]).GetValue("C_COUNTRY_ID").ToString() != "")
                    {
                        country = new MCountry(Envs.GetCtx(), Convert.ToInt32(((BindableObject)vdata[index]).GetValue("C_COUNTRY_ID")), null);
                    }
                    if (((BindableObject)vdata[index]).GetValue("POSTAL") != null && ((BindableObject)vdata[index]).GetValue("POSTAL").ToString() != "")
                    {
                        postal = ((BindableObject)vdata[index]).GetValue("POSTAL").ToString();
                    }
                    if (((BindableObject)vdata[index]).GetValue("NAME") != null && ((BindableObject)vdata[index]).GetValue("NAME").ToString() != "")
                    {
                        name = ((BindableObject)vdata[index]).GetValue("NAME").ToString();
                    }
                    if (((BindableObject)vdata[index]).GetValue("SUBNAME") != null && ((BindableObject)vdata[index]).GetValue("SUBNAME").ToString() != "")
                    {
                        subname = ((BindableObject)vdata[index]).GetValue("SUBNAME").ToString();
                    }
                    if (((BindableObject)vdata[index]).GetValue("CITYNAME") != null && ((BindableObject)vdata[index]).GetValue("CITYNAME").ToString() != "")
                    {
                        city = ((BindableObject)vdata[index]).GetValue("CITYNAME").ToString();
                    }
                    CityName = city;
                    Region = region;
                    Country = country;
                    Name = name;
                    Postal = postal;
                    SubName = subname;
                }
                Dispatcher.BeginInvoke(delegate
                {
                    this.DialogResult = true;
                });
            });
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void vdgvLocationSet_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString().Equals("C_COUNTRY_ID", StringComparison.OrdinalIgnoreCase))
            {
                e.Column.Visibility = Visibility.Collapsed;
            }
            if (e.Column.Header.ToString().Equals("C_REGION_ID", StringComparison.OrdinalIgnoreCase))
            {
                e.Column.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

        #region Properties
      
        public MRegion Region
        {
            get
            {
                return region;
            }
            set
            {
                region = value;
            }
        }

        public MCountry Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
            }
        }

        public string Postal
        {
            get
            {
                return postal;
            }
            set
            {
                postal = value;
            }

        }

        public string Name
        {
            get 
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string SubName
        {
            get
            {
                return subname;
            }
            set
            {
                subname = value;
            }
        }

        public string CityName
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
            }
        }
        #endregion
    }
}

